package content

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import cn.hutool.core.codec.Base64
import cn.hutool.core.img.ImgUtil
import cn.hutool.core.util.StrUtil
import cn.hutool.extra.qrcode.QrCodeUtil
import cn.hutool.extra.qrcode.QrConfig
import theme.divider
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import javax.swing.JFileChooser

/**
 * @author zooy
 * @create 2024-01-08 2:41
 * @description
 */
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PicToBase64Content() {

    Row(
        modifier = Modifier
           .fillMaxSize(),
    ) {

        val resultText = remember { mutableStateOf("") }
        val imagePath = remember { mutableStateOf("") }

        Column(
            modifier = Modifier.width(300.dp)
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            // 文件上传
            Button(onClick = {
                JFileChooser().apply {
                    fileSelectionMode = JFileChooser.FILES_ONLY
                    showOpenDialog(ComposeWindow())
                    selectedFile?.let {
                        val result = Base64.encode(it.readBytes())
                        resultText.value = result
                        imagePath.value = it.absolutePath
                    }
                }
            }) {
                Text(text = "选择图片")
            }

            if (StrUtil.isNotEmpty(imagePath.value)) {
                // 得到实际路径
                Image(
                    modifier = Modifier
                        .width(300.dp)
                        .padding(10.dp),
                    contentScale = ContentScale.Fit,
                    contentDescription = "图片",
                    painter = painterResource(imagePath.value, LocalFileResourceLoader()),
                    alignment = Alignment.Center
                )
            }
        }

        VerticalDivider(color = divider)

        OutlinedTextField(
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp)
                .fillMaxHeight(),
            value = resultText.value,
            label = { Text(text = "结果字符串") },
            onValueChange = {
            },
            textStyle = MaterialTheme.typography.body2.copy(
                fontWeight = FontWeight.ExtraLight
            ),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = MaterialTheme.colors.surface,
                cursorColor = Color.White,
                leadingIconColor = Color.White,
                trailingIconColor = Color.White,
                textColor = Color.White
            )
        )

    }

}

@Composable
fun VerticalDivider(
    modifier: Modifier = Modifier,
    color: Color = MaterialTheme.colors.onSurface.copy(alpha = 0.12f),
    thickness: Dp = 1.dp,
    startIndent: Dp = 0.dp
) {
    val indentMod = if (startIndent.value != 0f) {
        Modifier.padding(start = startIndent)
    } else {
        Modifier
    }
    Box(
        modifier.then(indentMod)
            .fillMaxHeight()
            .width(thickness)
            .background(color = color)
    )
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TextToQrcode() {

    Row(
        modifier = Modifier
            .fillMaxSize(),
    ) {

        val resultText = remember { mutableStateOf("") }

        val base64Text = remember { mutableStateOf("") }

        Column(
            modifier = Modifier.width(300.dp)
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            OutlinedTextField(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
                    .fillMaxHeight(),
                value = resultText.value,
                label = { Text(text = "文本") },
                onValueChange = {
                    resultText.value = it
                    if (!it.isNullOrEmpty()) {
                        val img = QrCodeUtil.generate(it, QrConfig(300, 300).setImg("tomato.png"))
                        base64Text.value = ImgUtil.toBase64(img, ImgUtil.IMAGE_TYPE_PNG)
                    }
                },
                textStyle = MaterialTheme.typography.body2.copy(
                    fontWeight = FontWeight.ExtraLight
                ),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = MaterialTheme.colors.surface,
                    cursorColor = Color.White,
                    leadingIconColor = Color.White,
                    trailingIconColor = Color.White,
                    textColor = Color.White
                )
            )
        }

        VerticalDivider(color = divider)

        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {

            if (!base64Text.value.isNullOrEmpty()) {
                Image(
                    modifier = Modifier
                        .width(300.dp)
                        .padding(10.dp),
                    contentScale = ContentScale.Fit,
                    contentDescription = "图片",
                    painter = painterResource(base64Text.value, ImageResourceLoader()),
                    alignment = Alignment.Center
                )
            }
        }

    }
}

@ExperimentalComposeUiApi
class ImageResourceLoader : ResourceLoader {

    override fun load(resourcePath: String): InputStream {
        return ByteArrayInputStream(Base64.decode(resourcePath))
    }
}

@ExperimentalComposeUiApi
class LocalFileResourceLoader : ResourceLoader {

    override fun load(resourcePath: String): InputStream {
        return FileInputStream(File(resourcePath))
    }
}
